<template>
  <div class="p-2">
    <transition :enter-active-class="proxy?.animate.searchAnimate.enter"
                :leave-active-class="proxy?.animate.searchAnimate.leave">
      <div v-show="showSearch" class="mb-[10px]">
        <el-card shadow="never">
          <el-form ref="queryFormRef" :model="queryPageDTO" :inline="true">
            <el-form-item label="年-月" prop="contrastDate">
              <el-date-picker
                value-format="YYYY-MM"
                v-model="queryPageDTO.contrastDate"
                type="month"
              />
            </el-form-item>
            <el-form-item label="预算部门编码" prop="deptCode" label-width="100">
              <el-input v-model="queryPageDTO.deptCode" placeholder="请输入预算部门编码" clearable
                        @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="预算部门名称" prop="deptName" label-width="100">
              <el-input v-model="queryPageDTO.deptName" placeholder="请输入预算部门名称" clearable
                        @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item>
              <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
              <el-button icon="Refresh" @click="resetQuery">重置</el-button>
            </el-form-item>
          </el-form>
        </el-card>
      </div>
    </transition>
    <el-card shadow="never">
      <template #header>
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button type="primary" plain icon="Pointer" @click="handleAdd(queryPageDTO.contrastDate)"
                       v-hasPermi="['budget:business:add']">生成
            </el-button>
          </el-col>
          <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>
      </template>

      <el-table border v-loading="loading" :data="businessList" class="enhanced-table">
        <el-table-column label="主键id" align="center" prop="id" v-if="false" />
        <el-table-column label="年-月" align="center" prop="contrastDate" />
        <el-table-column label="预算部门编码" align="center" prop="deptCode" />
        <el-table-column label="预算部门名称" align="center" prop="deptName" />
        <el-table-column label="预算科目编码" align="center" prop="subjectCode" />
        <el-table-column label="预算科目名称" align="center" prop="subjectName" />
      </el-table>

      <pagination v-show="total > 0" :total="total" v-model:page="queryPageDTO.pageNum"
                  v-model:limit="queryPageDTO.pageSize" @pagination="getList" />
    </el-card>
  </div>
</template>

<script setup name="Business" lang="ts">
import { addBusiness, listBusiness, verifyCount } from "@/api/budget/business";
import { BusinessQuery, BusinessVO } from "@/api/budget/business/types";
import useDate from "@/hooks/useDate";

const { proxy } = getCurrentInstance() as ComponentInternalInstance;

const businessList = ref<BusinessVO[]>([]);
const loading = ref(true);
const showSearch = ref(true);
const total = ref(0);

const queryFormRef = ref<ElFormInstance>();

const dialog = reactive<DialogOption>({
  visible: false,
  title: ""
});

//分业列表查询参数
const queryPageDTO = ref<BusinessQuery>({
  pageNum: 1,
  pageSize: 10,
  deptCode: undefined,
  deptName: undefined,
  contrastDate: useDate.getNow()
});


/** 查询业务部门科目设置列表 */
const getList = async () => {
  loading.value = true;
  const res = (await listBusiness(queryPageDTO.value)).data;
  businessList.value = res.records;
  total.value = res.total;
  loading.value = false;
};

/** 取消按钮 */
const cancel = () => {
  dialog.visible = false;
};

/** 搜索按钮操作 */
const handleQuery = () => {
  queryPageDTO.value.pageNum = 1;
  getList();
};

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value?.resetFields();
  handleQuery();
};

/** 生成按钮操作 */
const handleAdd = (contrastDate: string) => {
  let msg = "";
  verifyCount(contrastDate).then(res => {
    if (res.data) {
      msg = "是否生成$业务部门与预算科目对应关系?".replace("$", contrastDate);
    } else {
      msg = "已生成$业务部门与预算科目对应关系,是否覆盖?".replace("$", contrastDate);
    }
  }).then(() => {
    proxy?.$modal.confirm(msg).then(() => {
      addBusiness(contrastDate).then(() => {
        proxy?.$modal.msgSuccess("生成成功");
        getList();
      });
    });
  });
};


onMounted(() => {
  getList();
});
</script>
<style lang="scss" scoped>

</style>
